<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="hall-online-box animate__animated"
      :class="dynamicClassName"
      style="width: 880px;height: 274px;"
      :id="dynamicChildName()"
    >
      <h1 class="h1_title h1_title_long">
        <span>线上业务</span>
      </h1>
      <div class="yewu">
        <div class="yewu-item">
          <h2 class="h2_title"><span>线上运行</span></h2>
          <div>
            <p class="yw-name">线上渠道业务</p>
            <p class="num"><em>2096</em>件</p>
          </div>
          <div>
            <p class="yw-name">线上渠道业务占比</p>
            <p class="num"><em>20.96</em>%</p>
          </div>
        </div>
        <div class="yewu-item">
          <h2 class="h2_title"><span>运行成效</span></h2>
          <div>
            <p class="yw-name">线上渠道办件</p>
            <p class="num"><em>2096</em>件</p>
          </div>
          <div>
            <p class="yw-name">线上渠道办件占比</p>
            <p class="num"><em>20.96</em>%</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
export default {
  //混入封装方法
  mixins: [encapsulation],
  data: function() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.hall-online-box {
  width: 880px;
  height: 274px;
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  background-image: url(../../../thumbnail/backImg/new-border-middle-green.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  transform-origin: left top;
}

.yewu {
  display: flex;
  margin-top: 24px;

  .yewu-item {
    width: 50%;
    display: flex;
    flex-wrap: wrap;

    .h2_title {
      width: 100%;
    }

    &:last-of-type {
      & > div {
        &::after {
          background-image: url(../../static/assets/HallOnline/greenbg.png);
        }

        em {
          color: #00fff6;
        }
      }
    }

    & > div {
      position: relative;
      margin-top: 21px;
      width: 50%;
      text-align: center;
      height: 117px;

      &::after {
        content: "";
        position: absolute;
        inset: 0;
        background-image: url(../../static/assets/HallOnline/bluebg.png);
        background-repeat: no-repeat;
        background-position: center bottom;
        animation: scale-up-center 2s cubic-bezier(0.39, 0.575, 0.565, 1)
          infinite both;
      }
    }

    .yw-name {
      font-size: 16px;
      line-height: 24px;
    }

    .num {
      em {
        color: #00c2ff;
      }
    }
  }
}

@keyframes scale-up-center {
  0% {
    -webkit-transform: scale(0.95);
    transform: scale(0.95);
    opacity: 0.8;
  }

  100% {
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 1;
  }
}
</style>
